package com.ocn.li.scmdy.modules.app.utils;

import com.horizon.workflow.webservice.client.common.WorkFlowServiceFactory;
import com.horizon.workflow.webservice.client.operatorwork.IOperatorWork;
import com.horizon.workflow.webservice.client.operatorwork.NodeBean;
import com.horizon.workflow.webservice.client.operatorwork.WorkBean;
import com.horizon.workflow.webservice.client.operatorwork.XmlWorkFaceBean;
import com.horizon.workflow.webservice.client.todo.ITodo;
import org.apache.log4j.Logger;
import org.springframework.util.CollectionUtils;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OperatorWorkUtils {
    private static final Logger logger = Logger.getLogger(OperatorWorkUtils.class);
    // 流程实例操作对象接口
    public IOperatorWork iwc = null;
    // 操作后返回信息
    private WorkBean wb = new WorkBean();
    public static Map<String, String> map = null;
    public static Map<String, WorkBean> workBeanMap = null;
    public static Map<String, String> trackIdMap = null;
    // 操作后返回信息
    public static WorkBean wbc = null;
    //表单编号
    public static String formid_hz = "";
    public static String workid_hz = "";
    //业务数据表
    public static String tableName = "";
    //页面名称
    public static String pageName = "";
    public static WorkBean wbH = null;
    //处理多分支多人员
    public static WorkBean wbNodes = null;
    public static XmlWorkFaceBean xfb = null;

    public OperatorWorkUtils(String flowurl) {
        try {
            // 获取流程实例操作对象ConfigLoader.flowurl
            logger.info("获取慧正流程实例操作对象开始");
            iwc = WorkFlowServiceFactory.getOperatorWork(flowurl, "admin");
            logger.info("获取慧正流程实例操作对象结束");
            if (map == null) {
                map = new HashMap<String, String>();
            }
            if (null == workBeanMap) {
                workBeanMap = new HashMap<String, WorkBean>();
            }
            if (null == trackIdMap) {
                trackIdMap = new HashMap<String, String>();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 打开流程实例
    public WorkBean open(XmlWorkFaceBean xfb) {
        logger.info("打开流程实例开始");
        logger.info("入参------>workId:[" + xfb.getWorkid() + "],userId:[" + xfb.getUserid() + "],userName:[" + xfb.getUsername() + "],trackId:[" + xfb.getTrackid() + "]");
        wb = iwc.open(xfb);
        logger.info("出参------>result:[" + wb.getResult() + "],msg:[" + wb.getBackMsg() + "]");
        logger.info("打开流程实例结束");
        return wb;
    }

    /**
     * 创建流程实例
     */
    public WorkBean create(String userId, String flowId) {
        XmlWorkFaceBean bean = new XmlWorkFaceBean();
        bean.setUserid(userId);
        bean.setFlowid(flowId);
        logger.info("创建流程实例开始");
        logger.info("入参------>workId:[" + bean.getWorkid() + "],userId:[" + bean.getUserid() + "],userName:[" + bean.getUsername() + "],trackId:[" + bean.getTrackid() + "]");
        wb = iwc.create(bean);
        logger.info("出参------>result:[" + wb.getResult() + "],msg:[" + wb.getBackMsg() + "]");
        logger.info("创建流程实例结束");
        return wb;
    }

    public ITodo dbListHZ(String flowurl) throws MalformedURLException {
        ITodo it = WorkFlowServiceFactory.getTodo(flowurl, "admin");
        return it;
    }

    /**
     * 实例提交
     */
    public WorkBean submit(XmlWorkFaceBean xw) {
        xw.setMsgSendFlag("Todo");
        logger.info("提交流程实例开始");
        logger.info("入参------>workId:[" + xw.getWorkid() + "],userId:[" + xw.getUserid() + "],userName:[" + xw.getUsername() + "],trackId:[" + xw.getTrackid() + "]" + ",nodeId:[" + xw.getNodeid() + "],nextNodeId:[" + xw.getNextNodeid() + "],selectAutor:[" + xw.getSelectAuthor() + "],comments:[" + xw.getComments() + "]");
        wb = iwc.submit(xw);
        logger.info("出参------>result:[" + wb.getResult() + "],msg:[" + wb.getBackMsg() + "]");
        logger.info("提交流程实例结束");
        return wb;
    }

    public WorkBean getNextNode(XmlWorkFaceBean xw) {
        logger.info("获取下一节点开始");
        logger.info("入参------>workId:[" + xw.getWorkid() + "],userId:[" + xw.getUserid() + "],userName:[" + xw.getUsername() + "],trackId:[" + xw.getTrackid() + "]" + ",nodeId:[" + xw.getNodeid() + "],nextNodeId:[" + xw.getNextNodeid() + "],selectAutor:[" + xw.getSelectAuthor() + "],comments:[" + xw.getComments() + "]");
        WorkBean wbNode = iwc.getNextNode(xw);
        wbc = wbNode;
        logger.info("出参------>result:[" + wbc.getResult() + "],msg:[" + wbc.getBackMsg() + "]");
        logger.info("获取下一节点结束");
        return wbNode;
    }

    public WorkBean getNextNode1(XmlWorkFaceBean xw) {
        logger.info("获取下一节点开始");
        logger.info("入参------>workId:[" + xw.getWorkid() + "],userId:[" + xw.getUserid() + "],userName:[" + xw.getUsername() + "],trackId:[" + xw.getTrackid() + "]" + ",nodeId:[" + xw.getNodeid() + "],nextNodeId:[" + xw.getNextNodeid() + "],selectAutor:[" + xw.getSelectAuthor() + "],comments:[" + xw.getComments() + "]");
        WorkBean wbNode = iwc.getNextNode(xw);
        wbNodes = wbNode;
        logger.info("出参------>result:[" + wbNodes.getResult() + "],msg:[" + wbNodes.getBackMsg() + "]");
        logger.info("获取下一节点结束");
        return wbNode;
    }

    //关闭流程
    public int closeHZ(String fields) {
        XmlWorkFaceBean xbf = new XmlWorkFaceBean();
        xbf.setWorkid(fields.split(",")[0]);
        xbf.setNodeid(fields.split(",")[1]);
        xbf.setTrackid(fields.split(",")[2]);
        xbf.setUserid(fields.split(",")[3]);
        logger.info("关闭流程实例开始");
        logger.info("入参------>workId:[" + xbf.getWorkid() + "],userId:[" + xbf.getUserid() + "],userName:[" + xbf.getUsername() + "],trackId:[" + xbf.getTrackid() + "]" + ",nodeId:[" + xbf.getNodeid() + "],nextNodeId:[" + xbf.getNextNodeid() + "],selectAutor:[" + xbf.getSelectAuthor() + "],comments:[" + xbf.getComments() + "]");
        wb = iwc.close(xbf);
        logger.info("出参------>result:[" + wb.getResult() + "],msg:[" + wb.getBackMsg() + "]");
        logger.info("关闭流程实例结束");
        return wb.getResult();
    }

    /**
     * 暂存并且管理流程实例
     */
    public WorkBean saveAndClose(XmlWorkFaceBean xfb) {
        logger.info("暂存流程实例开始");
        logger.info("入参------>workId:[" + xfb.getWorkid() + "],userId:[" + xfb.getUserid() + "],userName:[" + xfb.getUsername() + "],trackId:[" + xfb.getTrackid() + "]" + ",nodeId:[" + xfb.getNodeid() + "],nextNodeId:[" + xfb.getNextNodeid() + "],selectAutor:[" + xfb.getSelectAuthor() + "],comments:[" + xfb.getComments() + "]");
        wb = iwc.saveAndClose(xfb);
        logger.info("出参------>result:[" + wb.getResult() + "],msg:[" + wb.getBackMsg() + "]");
        logger.info("暂存流程实例结束");
        return wb;
    }

    /**
     * 拿回
     *
     * @param xfb
     */
    public WorkBean getBack(XmlWorkFaceBean xfb) {
        xfb.setMsgSendFlag("Todo");
        logger.info("拿回流程实例开始");
        logger.info("入参------>workId:[" + xfb.getWorkid() + "],userId:[" + xfb.getUserid() + "],userName:[" + xfb.getUsername() + "],trackId:[" + xfb.getTrackid() + "]" + ",nodeId:[" + xfb.getNodeid() + "],nextNodeId:[" + xfb.getNextNodeid() + "],selectAutor:[" + xfb.getSelectAuthor() + "],comments:[" + xfb.getComments() + "]");
        wb = iwc.getBack(xfb);
        logger.info("出参------>result:[" + wb.getResult() + "],msg:[" + wb.getBackMsg() + "]");
        logger.info("拿回流程实例结束");
        return wb;
    }

    /**
     * 终止
     *
     * @param xfb
     */
    public WorkBean stop(XmlWorkFaceBean xfb) {
        logger.info("终止流程实例开始");
        logger.info("入参------>workId:[" + xfb.getWorkid() + "],userId:[" + xfb.getUserid() + "],userName:[" + xfb.getUsername() + "],trackId:[" + xfb.getTrackid() + "]" + ",nodeId:[" + xfb.getNodeid() + "],nextNodeId:[" + xfb.getNextNodeid() + "],selectAutor:[" + xfb.getSelectAuthor() + "],comments:[" + xfb.getComments() + "]");
        wb = iwc.stop(xfb);
        logger.info("出参------>result:[" + wb.getResult() + "],msg:[" + wb.getBackMsg() + "]");
        logger.info("终止流程实例结束");
        return wb;
    }

    /**
     * 实例退回
     */
    public List<Object> reject(XmlWorkFaceBean xfb, String preNodeId) {
        logger.info("退回流程实例开始");
        logger.info("入参------>workId:[" + xfb.getWorkid() + "],userId:[" + xfb.getUserid() + "],userName:[" + xfb.getUsername() + "],trackId:[" + xfb.getTrackid() + "]" + ",nodeId:[" + xfb.getNodeid() + "],nextNodeId:[" + xfb.getNextNodeid() + "],selectAutor:[" + xfb.getSelectAuthor() + "],comments:[" + xfb.getComments() + "]");
        List<NodeBean> list = iwc.getRejectNode(xfb);
        logger.info("list大小：" + (!CollectionUtils.isEmpty(list) ? list.size() : 0));
        if (!CollectionUtils.isEmpty(list)) {
            for (NodeBean nodeBean : list) {
                logger.info("nodeId:[" + nodeBean.getNodeId() + "],nodeName:[" + nodeBean.getNodeName() + "]");
            }
        }
        logger.info("退回流程实例结束");
        String nodeName = "";
        if (null != list && list.size() > 0) {
            for (NodeBean b : list) {
                System.out.println(b.getNodeId() + "===" + b.getNodeName());
                if (b.getNodeId().equals(preNodeId)) {
                    xfb.setNextNodeid(preNodeId);
                    nodeName = b.getNodeName();
                }
            }
        }
        List<Object> obj = new ArrayList<Object>();
        if (!"".equals(nodeName)) {
            String node = xfb.getNextNodeid();
            wb = iwc.reject(xfb);
            obj.add(wb.getResult());
            obj.add(wb.getBackMsg());
            obj.add(node);
            obj.add(nodeName);
        } else {
            obj.add("-1");
            obj.add("未配置该退回节点，请联系管理员或开发人员在流程定制中配置！");
        }
        return obj;
    }

    /**
     * 暂停/恢复
     *
     * @param xfb
     */
    public WorkBean pause(XmlWorkFaceBean xfb) {
        xfb.setMsgSendFlag("Todo");
        wb = iwc.pause(xfb);
        return wb;
    }

}
